//
//  ViewController.m
//  26-GCD
//
//  Created by Snow on 16/8/18.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.GCD:Grand(伟大的) Central(中央的) Dispatch(调度)
    //2.GCD的核心概念:队列, 任务拆分分配到队列中, 系统会自动为我们创建线程和调度线程
    //3.GCD中的队列严格遵循FIFO (先进先出原则)
    
    //队列分类//
        //1.串行 DISPATCH_QUEUE_SERIAL / NULL
        //2.并发 DISPATCH_QUEUE_CONCURRENT
    
    //4.队列的创建
        //参数1:队列名字
        //参数2:队列类型
    dispatch_queue_t serialqueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);
    
    dispatch_queue_t concurrentqueue = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
    //主队列, 串行
    dispatch_queue_t mainqueue = dispatch_get_main_queue();
    //全局队列
    //参数1: 调度队列优先级
    //参数2: 给未来使用, 占位使用, 填0
    dispatch_queue_t globalqueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //术语:
    //串行, 一个任务执行完毕, 再执行下一个
    //并发, 多个任务同时执行
    
    //5.同步, 异步
    //同步:在当前线程执行任务, 不能开辟线程
    //异步:在新的线程执行任务, 可以开辟线程
    
        //1>同步 串行队列 不会创建线程
//    [self syncSerialQueue:serialqueue];
    //同步并发队列
//    [self syncConcurrentQeue:concurrentqueue];
    //同步主线程队列
    //主线程 同步 主线程
    //主线程等待同步操作结束, 同步操作在主线程执行 互相等待 导致程序卡死
//    [self syncMainQueue:mainqueue];
    // 同步 全局队列 并发
//    [self syncGlobalQueue:globalqueue];
    
        //2>异步
    //异步串行队列/ 创建一个新的线程, 在该线程中执行
    [self asyncSerialQueue:serialqueue];
    //异步并发队列 根据任务决定需要创建多少线程
//    [self asyncConcurrentQueue:concurrentqueue];
    //异步 主线程队列
//    [self asyncMainQueue:mainqueue];
    //异步 全局队列 并发
//    [self asyncGlobalQueue:globalqueue];
    
    //6.group 组
        //1>创建
    dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
        //2>组只有异步
        //组中添加多个任务
//    dispatch_group_t group = dispatch_group_create();
//    dispatch_group_async(group, serialQueue, ^{
//        NSLog(@"%@---1", [NSThread currentThread]);
//    });
//    dispatch_group_async(group, serialQueue, ^{
//        NSLog(@"%@---2", [NSThread currentThread]);
//    });
//    dispatch_group_async(group, serialQueue, ^{
//        NSLog(@"%@---3", [NSThread currentThread]);
//    });
//
//    dispatch_group_notify(group, mainqueue, ^{
//        //所有操作完成时, 调用下列方法
//        NSLog(@"左右操作完成时, 施行后继操作 刷新");
//    });
//    [self asyncGroupEnter];

//    dispatch_group_wait(group, DISPATCH_TIME_NOW);
//    dispatch_function_t work;
//    dispatch_group_async_f(group, mainqueue, nil, work);
    
    
}
-(void)asyncGroupEnter
{
    // 群组－统一监控一组任务
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    // 1> 入组 -> 之后的 block 会被 group 监听
    // dispatch_group_enter 一定和 dispatch_group_leave 要配对出现
    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        NSLog(@"dispatch_async_work1");
        
        // block 的末尾，所有任务执行完毕后，添加一个出组
        dispatch_group_leave(group);
    });
    
    //  再次入组
    dispatch_group_enter(group);
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:6];
        
        NSLog(@"dispatch_async_work1");
        
        // block 的末尾，所有任务执行完毕后，添加一个出组
        dispatch_group_leave(group);
    });
    
    // 群组结束
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"OVER");
    });  
    NSLog(@"come here");
}
- (void)syncSerialQueue:(dispatch_queue_t)serialQueue
{
    //block中的 会追加到队列中
    for (int i = 0; i < 10; i++) {
        dispatch_sync(serialQueue, ^{
            NSLog(@"同步串行队列:%d -- %@", i, [NSThread currentThread]);
        });
    }
}
- (void)syncConcurrentQeue:(dispatch_queue_t)concurrentQueue
{
    for (int i = 0; i < 10; i++) {
        dispatch_sync(concurrentQueue, ^{
            NSLog(@"同步并发队列:%d -- %@", i, [NSThread currentThread]);
        });
    }
}
- (void)syncMainQueue:(dispatch_queue_t)mainQueue
{
//    for (int i = 0; i < 10; i++) {
//        dispatch_sync(mainQueue, ^{
//            NSLog(@"同步主线程队列:%d -- %@", i, [NSThread currentThread]);
//        });
//    }
}
- (void)syncGlobalQueue:(dispatch_queue_t)globalQueue
{
    for (int i = 0; i < 10; i++) {
        dispatch_sync(globalQueue, ^{
            NSLog(@"同步全局队列:%d -- %@", i, [NSThread currentThread]);
        });
    }
}
//异步串行队列 创建一个新的线程, 在该线程中执行
- (void)asyncSerialQueue:(dispatch_queue_t)serialQueue
{
    for (int i = 0; i < 10; i++) {
        dispatch_async(serialQueue, ^{
            NSLog(@"异步串行队列 %d -- %@", i, [NSThread currentThread]);
        });
    }
}
//异步并发队列 创建新的线程
- (void)asyncConcurrentQueue:(dispatch_queue_t)concurrentQueue
{
    for (int i = 0; i < 10; i++) {
        dispatch_async(concurrentQueue, ^{
            NSLog(@"异步并发队列%d -- %@", i, [NSThread currentThread]);
        });
    }
}
- (void)asyncMainQueue:(dispatch_queue_t)mainQueue
{
    for (int i = 0; i < 10; i++) {
        dispatch_async(mainQueue, ^{
            NSLog(@"异步主线程队列:%d -- %@", i, [NSThread currentThread]);
        });
    }
}
- (void)asyncGlobalQueue:(dispatch_queue_t)globalQueue
{
    for (int i = 0; i < 10; i++) {
        dispatch_async(globalQueue, ^{
            NSLog(@"异步全局队列:%d -- %@", i, [NSThread currentThread]);
        });
    }
}
- (IBAction)picOfGCD:(id)sender {
    dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(serialQueue, ^{
        //耗时
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/8/511ce1c9ee276.jpg"]];
        //返回主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            _imageView.image = [UIImage imageWithData:data];
        });
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
